<?php
	namespace Common\Logic;
	use Common\Model\OrderCartModel;
	class OrderCartLogic extends OrderCartModel{
		/*
		* Function:		getOrderCartList
		* Description:	订单购物车列表
		* @param:		$page string 起始页
						$filter array 过滤数组
						$pageSize string 每页显示条数
		*/
		public function getOrderCartList($page=1,$filter = '',$pageSize=20){
			$conditon = $this->getOrderCartFilter($filter);
			return $this->where($conditon)->order('id desc')->limit(($page-1)*$pageSize,$pageSize)->select();
		}
		
		/*
		* Function:get:	getOrderCartFilter
		* Description:	订单购物车列表搜索过滤项
		* @param:		$filter array 过滤数组
		*/
		public function getOrderCartFilter($filter = ''){
			$conditon = array();
			if(!empty($filter['name'])){
				$conditon['name'] = array('like','%'.$filter['name'].'%');
			}
			return $conditon;
		}
		
		/*
		* Function:get:	getOrderCartCount
		* Description:	订单购物车列表总数
		* @param:		$filter array 过滤数组
		*/
		public function getOrderCartCount($filter = ''){
			$conditon = $this->getOrderCartFilter($filter);
			return $this->where($conditon)->count();
		}
		
		/*
		* Function:		addOrderCart
		* Description:	增加订单购物车
		* @param:		$data array 增加的数组
		*/
		public function addOrderCart($data){
			return $this->add($data);
		}
		
		/*
		* Function:		getOrderCartInfo
		* Description:	获取订单购物车信息 by row
		* @param:		$id int ID
		*/
		public function getOrderCartInfo($id){
			return $this->where(array('id'=>$id))->find();
		}
		
		/*
		* Function:		updateOrderCart
		* Description:	更新订单购物车
		* @param:		$data array 更新的数组
		*/
		public function updateOrderCart($data){
			return $this->save($data);
		}
		
		/*
		* Function:		deleteOrderCart
		* Description:	删除订单购物车
		* @param:		$id int ID
		*/
		public function deleteOrderCart($id){
			return $this->where(array('id'=>$id))->delete();
		}
		
		/*
		* Function:		getOrderCartAllList
		* Description:	部门列表
		*/
		public function getOrderCartAllList(){
			return $this->where(array('status'=>1))->select();
		}

		/*
		* Function:		orderCart
		* Description:	购物车功能
		*/
		public function orderCart($data){
			$return = array();
			//判断商品是否上架
			if($goodsInfo = M('goods')->where(['id'=>$data['goods_id']])->find()){
				if(empty($goodsInfo['status'])){
					return ['code'=>0,'errorMsg'=>'此商品已下架'];
				}
			}else{
				return ['code'=>0,'errorMsg'=>'不存在此商品'];
			}
			if(empty($data['goods_spec_id'])){
				$goodsListInfo = M('goods_list')->where(['id'=>$data['goods_list_id']])->find();
			}else{
				$goodsListInfo = M('goods_list')->where(['goods_id'=>$data['goods_id'],'goods_spec_id'=>$data['goods_spec_id']])->find();
			}
			if(!empty($goodsListInfo)){
				if(empty($goodsListInfo['status'])){
					return ['code'=>0,'errorMsg'=>'此商品规格已下架'];
				}
			}else{
				return ['code'=>0,'errorMsg'=>'不存在此商品规格'];
			}
			if($data['nums']<=0){
				return ['code'=>0,'errorMsg'=>'商品数量有误'];
			}
			//判断商品库存是否够
			if(($goodsListInfo['stocks']-$goodsListInfo['freez']-$data['nums'])<0){
				return ['code'=>0,'errorMsg'=>'此商品库存不足'];
			}
			$cartInfo = $this->where(['type'=>1,'user_id'=>$data['user_id'],'goods_id'=>$data['goods_id'],'goods_spec_id'=>$data['goods_spec_id'],'status'=>1])->find();
			if(!empty($cartInfo)){
				$data['nums'] = $data['nums']+$cartInfo['nums'];
				$ret = $this->where(['type'=>1,'user_id'=>$data['user_id'],'goods_id'=>$data['goods_id'],'goods_list_id'=>$data['goods_list_id'],'goods_spec_id'=>$data['goods_spec_id']])->save($data);
			}else{
				$ret = $this->add($data);
				$ret = !empty($ret)?true:false;
			}
         if($ret!==false){
            return ['code'=>1,'errorMsg'=>'操作成功'];
         }else{
            return ['code'=>0,'errorMsg'=>'操作失败'];
         }
		}

		/*
		* Function:		delOrderCart
		* Description:	购物车功能-删除
		*/
		public function delOrderCart($data){
			$return = array();
			$ret = $this->where(['type'=>1,'id'=>['in',$data['id']]])->delete();
			if(empty($ret)){
				return ['code'=>0,'errorMsg'=>'删除失败'];
			}
			return ['code'=>1,'errorMsg'=>'删除成功'];
		}

		/*
		* Function:		commitOrderCart
		* Description:	购物车-提交
		*/
		public function commitOrderCart($data){
			$return = [];
			$dataList = [];
			$dateTime = date('Y-m-d H:i:s',time());
			if(is_array($data['goods_data'])&&count($data['goods_data'])>0){
				foreach($data['goods_data'] as $key=>$val){
					//判断商品是否上架
					if($goodsInfo = M('goods')->where(['id'=>$val['goods_id']])->find()){
						if(empty($goodsInfo['status'])){
							return ['code'=>0,'errorMsg'=>'商品：'.$goodsInfo['goods_title'].'已下架'];
						}
					}else{
						return ['code'=>0,'errorMsg'=>'不存在此商品'];
					}
					if($goodsListInfo = M('goods_list')->where(['id'=>$val['goods_list_id']])->find()){
						if(empty($goodsListInfo['status'])){
							return ['code'=>0,'errorMsg'=>'商品：'.$goodsInfo['goods_title'].'('.$goodsListInfo['goods_spec'].')已下架'];
						}
					}else{
						return ['code'=>0,'errorMsg'=>'此商品:'.$goodsInfo['goods_title'].'规格不存在'];
					}
					if($val['nums']<=0){
						return ['code'=>0,'errorMsg'=>'商品：'.$goodsInfo['goods_title'].'数量有误'];
					}
					//判断商品库存是否够
					if(($goodsListInfo['stocks']-$goodsListInfo['freez']-$val['nums'])<0){
						return ['code'=>0,'errorMsg'=>'商品：'.$goodsInfo['goods_title'].'('.$goodsListInfo['goods_spec'].')库存不足'];
					}
					//查询当前购物车
					$val['addtime'] = $dateTime;
					$dataList[] = $val;
				}
			}
			if(is_array($dataList)&&count($dataList)>0){
				$this->startTrans();
				$ret = $this->where(['type'=>1,'user_id'=>$data['user_id']])->delete();
				if(empty($ret)){
					$this->rollback();
					return ['code'=>0,'errorMsg'=>'购物车异常'];
				}
				$ret = $this->addAll($dataList);
				if(empty($ret)){
					$this->rollback();
					return ['code'=>0,'errorMsg'=>'购物车更新异常'];
				}
				$this->commit();
				return ['code'=>1,'errorMsg'=>'success'];
			}
		}
	}
?>